#include "canswerform.h"
#include "ui_canswerform.h"
#include <QFocusEvent>
#include <QtGui>

CAnswerForm::CAnswerForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CAnswerForm)
{
    ui->setupUi(this);
    connect(ui->checkBox, SIGNAL(toggled(bool)), this, SIGNAL(rightAnswerChecked()));
    connect(ui->textEdit, SIGNAL(textChanged()), this, SIGNAL(answerChanged()));
    ui->textEdit->installEventFilter(this);
}

CAnswerForm::~CAnswerForm()
{
    delete ui;
}

bool CAnswerForm::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::FocusIn)
    {
        if (obj->objectName() == ui->textEdit->objectName())
        {
	    //textEdit получил фокус - сигнализируем
	    emit answerClicked(this);
            return QObject::eventFilter(obj, event);
        }
        return false;
    }
    else
    {
        return false;
    }

}

bool CAnswerForm::isRightAnswer() const
{
    return ui->checkBox->isChecked();
}

void CAnswerForm::setRightAnswer(bool check)
{
    ui->checkBox->setChecked(check);
}

QString CAnswerForm::getAnswer() const
{
    return ui->textEdit->toHtml();
}

QString CAnswerForm::getComment() const
{
    return QString();
}

void CAnswerForm::setAnswer(QString answer)
{
    ui->textEdit->setHtml(answer);
}

void CAnswerForm::reset()
{
    setRightAnswer(false);
    setAnswer(QString());
}

void CAnswerForm::activate()
{
    ui->textEdit->setFocus();
}
